package org.darcy.service.privilege.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.darcy.dao.privilege.SysRoleMenuMapper;
import org.darcy.entity.privilege.SysMenu;
import org.darcy.entity.privilege.SysRoleMenu;
import org.darcy.framework.generic.GenericServiceImpl;
import org.darcy.service.privilege.SysMenuService;
import org.darcy.service.privilege.SysRoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

@Service
public class SysRoleMenuServiceImpl extends GenericServiceImpl<SysRoleMenu> implements SysRoleMenuService {

	@Lazy
	@Autowired
	private SysMenuService SysMenuService;

	private List<SysRoleMenu> getListByRoleId(Integer roleId) {
		LambdaQueryWrapper<SysRoleMenu> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(SysRoleMenu::getRoleId, roleId);
		List<SysRoleMenu> list = super.getList(queryWrapper);
		return list;
	}

	@Override
	public List<SysMenu> getMenuList(Integer roleId) {
		List<SysRoleMenu> rolemenuList = this.getListByRoleId(roleId);
		List<SysMenu> allMenuList = SysMenuService.getAll(new SysMenu());
		List<SysMenu> result = new ArrayList<SysMenu>();
		for (SysMenu a : allMenuList) {
			boolean flag = false;
			for (SysRoleMenu b : rolemenuList) {
				if (b.getMenuId().equals(a.getId())) {
					flag = true;
				}
			}
			if (flag) {
				a.setChecked(true);
			}
			result.add(a);
		}

		return result;
	}

}
